﻿#include"string.h"
#define _CRT_SECURE_NO_WARNINGS


namespace bit {
	void test_string1()
	{
	

		bit::string s1("dweuybzxd");
		cout << s1.c_str() << endl;

		//访问：可写
		for (size_t i = 0; i < s1.size(); i++)
		{
			s1[i]++;
		}
		for (size_t i = 0; i < s1.size(); i++)
		{
			cout << s1[i] << " ";
		} 
		cout << endl;
		//无参
		/*bit::string s2;
		cout << s2.c_str() << endl;*/
		

		//迭代器访问
		//封装：屏蔽了底层实现细节，提供了一种简单通用访问容器的方式
		string::iterator it1 = s1.begin();
		while (it1 != s1.end())
		{
			cout << *it1 << " ";
			++it1;
		}
		cout << endl;

		//迭代器：
		//s1中的值以e输出
		for (auto e:s1)
		{
			cout << e << " ";
		}
		cout << endl;


		//const访问
		const bit::string s3("xxxxxxxxx");
		string::const_iterator it3 = s3.begin();
		while (it3 != s3.end())
		{
			//*it3 = 'y';
			//赋值报错/不可修改的const

			cout << *it3 << " ";
			++it3;
		}
		cout << endl;

		for (size_t i = 0; i < s1.size(); i++)
		{
			cout << s3[i] << " ";
		}
		cout << endl;
	}

	void test_string2()
	{
		bit::string s1("dweuybzxd");
		cout << s1.c_str() << endl;

		//s1.push_back('x');
		//s1.push_back('x');
		////追加
		//s1.append(" dxde ");
		//s1.push_back('x');

		s1 += 'x';
		s1 += "加等";
		cout << s1.c_str() << endl;
	}

	void test_string3()
	{
		bit::string s1("dweuybzxd");
		s1.insert(5,'x');
		//s1.insert(0,'x');//❌：越界访问：end--到-1了
		cout << s1.c_str() << endl;

		s1.insert(6," tjy ");
		cout << s1.c_str() << endl;

		s1.erase(6,3);
		cout << s1.c_str() << endl;
	}

	void test_string4()
	{
		bit::string s1("dweuybzxd");
		cout << s1.find('d',2) << endl;
		cout << s1.find("weu", 0) << endl;
	}

	void test_string5()  
	{
		
		bit::string s1("dweuybzxd");
		bit::string s2(s1);

		s1[0] = 'x';
		cout << s1.c_str() << endl;
		cout << s2.c_str() << endl;
		cout << endl;

		bit::string s3("yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy");
		s1 = s3;
		cout << s1.c_str() << endl;
		cout << s2.c_str() << endl;
		cout << s3.c_str() << endl;
		cout << endl;

		s1.swap(s2);
		cout << s1.c_str() << endl;
		cout << s2.c_str() << endl;
		cout << endl;

		bit::string url1 = s1.substr(3, 3);
		cout << url1.c_str() << endl;
	}

	void test_string7()
	{
		/*bit::string s1("hello world");
		cout << s1 << endl;*/

		bit::string s1;
		cin >> s1;
		cout << s1 << endl;
	}

	void test_string8()
	{
		bit::string s1("hello world");
		

		bit::string s2(s1);
		bit::string s3("hello！！！");

		s1 = s3;
		cout << s1 << endl;
		cout << s2 << endl;
		cout << s3 << endl;
	}

	void test_string9()
	{
		bit::string s1("hello world");
		bit::string s2(s1);

		/*std::string s1("hello world");
		std::string s2(s1);*/
		
		//测试浅拷贝：引用计数写时拷贝
		cout << (void*)s1.c_str() << endl;
		cout << (void*)s2.c_str() << endl;
		//显示底层的指针(字符型显示不出来，要转换类型)
		//如果指向的同一块空间：则浅拷贝成功/否则失败
	
		
	}
}

//int main()
//{
//	//bit::test_string1();
//	//bit::test_string2();
//	//bit::test_string3();
//	//bit::test_string4();
//	//bit::test_string5();
//	//bit::test_string7();
//	//bit::test_string8();
//	bit::test_string9();
//}